import Toast from 'tdesign-miniprogram/toast/index';
import Dialog from 'tdesign-miniprogram/dialog/index';
import {
  OrderButtonTypes,
  OrderStatus
} from '../../config';
import {
  revokeOrder,
  confimOrder,
  serviceOrder,
  refundOrder
} from '../../../../api/order';
Component({
  options: {
    addGlobalClass: true,
  },
  properties: {
    order: {
      type: Object,
      observer(order) {
        // 判定有传goodsIndex ，则认为是商品button bar, 仅显示申请售后按钮
        //if (this.properties?.goodsIndex !== null) {
        //const goods = order.goodsList[Number(this.properties.goodsIndex)];
        this.setData({
          buttons: {
            left: [],
            right: (order.buttons || []).filter((b) => b.type == OrderButtonTypes.APPLY_REFUND),
          },
        });
        //return;
        //}
        // 订单的button bar 不显示申请售后按钮
        const buttonsRight = (order.buttons || [])
          // .filter((b) => b.type !== OrderButtonTypes.APPLY_REFUND)
          .map((button) => {
            //邀请好友拼团按钮
            if (button.type === OrderButtonTypes.INVITE_GROUPON && order.groupInfoVo) {
              const {
                groupInfoVo: {
                  groupId,
                  promotionId,
                  remainMember,
                  groupPrice
                },
                goodsList,
              } = order;
              const goodsImg = goodsList[0] && goodsList[0].imgUrl;
              const goodsName = goodsList[0] && goodsList[0].name;
              return {
                ...button,
                openType: 'share',
                dataShare: {
                  goodsImg,
                  goodsName,
                  groupId,
                  promotionId,
                  remainMember,
                  groupPrice,
                  storeId: order.storeId,
                },
              };
            }
            return button;
          });
        // 删除订单按钮单独挪到左侧
        const deleteBtnIndex = buttonsRight.findIndex((b) => b.type === OrderButtonTypes.DELETE);
        let buttonsLeft = [];
        if (deleteBtnIndex > -1) {
          buttonsLeft = buttonsRight.splice(deleteBtnIndex, 1);
        }
        this.setData({
          buttons: {
            left: buttonsLeft,
            right: buttonsRight,
          },
        });
      },
    },
    goodsIndex: {
      type: Number,
      value: null,
    },
    isBtnMax: {
      type: Boolean,
      value: false,
    },
  },

  data: {
    order: {},
    buttons: {
      left: [],
      right: [],
    },
  },

  methods: {
    // 点击【订单操作】按钮，根据按钮类型分发
    onOrderBtnTap(e) {
      const {
        type
      } = e.currentTarget.dataset;
      switch (type) {
        case OrderButtonTypes.CONFIRM: //确认上车
          this.onConfirm(this.data.order);
          break;
        case OrderButtonTypes.REFUND: //没有发车退款
          this.onApplyRefund(this.data.order);
          break;
        case OrderButtonTypes.SERVICE: //已发车退款
          this.onServiceRefund(this.data.order);
          break;
        case OrderButtonTypes.REVOKE: //取消退款
          this.onRevoke(this.data.order);
          break;
      }
    },
    onConfirm(order) {
      Dialog.confirm({
          title: '确认是否已经上车？',
          content: '',
          confirmBtn: '确认上车',
          cancelBtn: '取消',
        })
        .then(() => {
          confimOrder({
            "id": order.id
          }).then((res) => {
            if (res.code == 400) {
              Toast({
                context: this,
                selector: '#t-toast',
                message: res.msg,
                icon: 'check-circle',
              });
            } else {
              Toast({
                context: this,
                selector: '#t-toast',
                message: '已确认上车',
                icon: 'check-circle',
              });
              this.triggerEvent("refresh");
            }
          }).catch(() => {
            Toast({
              context: this,
              selector: '#t-toast',
              message: '该票不存在',
              icon: 'check-circle',
            });
          })
        })
        .catch(() => {

        });
    },
    onApplyRefund(order) {
      Dialog.confirm({
          title: '确认是否不坐车了？',
          content: '',
          confirmBtn: '确认不坐了',
          cancelBtn: '取消',
        })
        .then(() => {
          refundOrder({
            "id": order.id,
            "status": OrderStatus.APPLY
          }).then((res) => {
            if (res.code == 400) {
              Toast({
                context: this,
                selector: '#t-toast',
                message: res.msg,
                icon: 'check-circle',
              });
            } else {
              Toast({
                context: this,
                selector: '#t-toast',
                message: '已确认上车',
                icon: 'check-circle',
              });
              this.triggerEvent("refresh");
            }
          }).catch(() => {
            Toast({
              context: this,
              selector: '#t-toast',
              message: '处理完成',
              icon: 'check-circle',
            });
          })
        })
        .catch(() => {

        });
    },
    onRevoke(order) {
      revokeOrder({
        "id": order.id
      }).then((res) => {
        if (res.code == 400) {
          Toast({
            context: this,
            selector: '#t-toast',
            message: res.msg,
            icon: 'check-circle',
          });
        } else {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '已取消退款',
            icon: 'check-circle',
          });
          this.triggerEvent("refresh");
        }
      }).catch(() => {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '处理完成',
          icon: 'check-circle',
        });
      })
    },

    onServiceRefund(order) {
      Dialog.confirm({
          title: '确认申请退款？',
          content: '',
          confirmBtn: '确认',
          cancelBtn: '取消',
        })
        .then(() => {
          serviceOrder({
            "id": order.id
          }).then((res) => {
            if (res.code == 400) {
              Toast({
                context: this,
                selector: '#t-toast',
                message: res.msg,
                icon: 'check-circle',
              });
            } else {
              Toast({
                context: this,
                selector: '#t-toast',
                message: '已申请',
                icon: 'check-circle',
              });
              this.triggerEvent("refresh");
            }
          }).catch(() => {
            Toast({
              context: this,
              selector: '#t-toast',
              message: '处理完成',
              icon: 'check-circle',
            });
          })
        })
        .catch(() => {

        });
    },
  },
});